﻿
using System.Net;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using Simple.Services;
using Simple.Services.System.QHSEDanger.Models.AnalysisInput;
/// <summary>
///交叉方隐患管理
/// </summary>
[Route("QHSEOverLappingDanger/[action]")]
[ApiController]
public class QHSEOverLappingDangerController : ControllerBase
{
    private readonly QHSEOverLappingDangerService _dangerService;
    public QHSEOverLappingDangerController(QHSEOverLappingDangerService dangerService)
    {
        _dangerService = dangerService;
    }
    //[HttpPost]
    //[ValidateTimestampAndSignature]
    //public async Task<AppResult> GetBySql([FromBody] IdInputModel model)
    //{
    //    List<QHSEOverLappingDangerModel> data = await _dangerService.GetBySqlAsync(model);
    //    return AppResult.Status200OK(data: data);
    //}
    /// <summary>
    /// 查询隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_page")]
    public async Task<AppResult> Page([FromQuery] OverLappingDangerPageInputModel model)
    {
        var dangers = await _dangerService.GetPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Page1([FromQuery] OverLappingDangerPageInputModel model)
    {
        var dangers = await _dangerService.GetPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    /// <summary>
    /// 新增隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_insert")]
    public async Task<AppResult> Add([FromBody] QHSEOverLappingDangerModel model)
    {
        var i = await _dangerService.AddAsync(model);
        return AppResult.Status200OK(data: i);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Add1([FromBody] QHSEOverLappingDangerModel model)
    {
        var i = await _dangerService.AddAsync(model);
        if (i == "请完善信息")
        {

            return AppResult.Status500InternalServerError(data: i);
        }
        return AppResult.Status200OK(data: i);
    }
    /// <summary>
    /// 隐患删除
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpDelete]
    [CustomAuthorize("hdv_qhse_d_all_delete")]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> model)
    {
        await _dangerService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    [HttpDelete]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Delete1([FromBody] List<IdInputModel> model)
    {
        await _dangerService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    /// <summary>
    /// 隐患编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_edit")]
    public async Task<AppResult> Edit([FromBody] QHSEOverLappingDangerModel model)
    {
        await _dangerService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Edit1([FromBody] QHSEOverLappingDangerModel model)
    {
        await _dangerService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    /// <summary>
    /// 隐患详情
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_querydetail")]
    public async Task<AppResult> QueryDetail([FromBody] IdInputModel inputID)
    {
        var data = await _dangerService.QueryDetailAsync(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> QueryDetail1([FromBody] IdInputModel inputID)
    {
        var data = await _dangerService.QueryDetailAsync(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 导出综合查询数据到excel
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<IActionResult> ExportToExcel([FromBody] OverLappingDangerPageInputModel model)
    {
        // 调用 ExcelService 中的方法导出 Excel
        var excelBytes = await _dangerService.GenerateExcel(model);

        var fileName = "danger_" + DateTime.Now + ".xlsx"; // 设置文件名

        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        return File(excelBytes, contentType, fileName);
    }

}
